// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: "dev-rp6i6"
}) //初始化数据库

// 云函数入口函数
exports.main = async (event, context) => {
  let updateRes;
  const db = cloud.database();
  const _ = db.command
  const collection = db.collection(event.collectionName)
  try {
    updateRes = await collection.doc(event._id)
      .update({
        data: {
          [event.key]: _.inc(1)
        },
      })
      .then(res => res)
      .catch(err => err)
  } catch (e) {
    throw new Error(e)
  }
  return {
    updateRes
  }
}